package com.app.shop.activitys;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.app.shop.R;
import com.app.shop.adapters.TalkAdapter;
import com.app.shop.beans.GoodsBean;
import com.app.shop.beans.ShopCarBean;
import com.app.shop.beans.TalkBean;
import com.app.library.base.BaseActivity;
import com.app.library.beans.EventMessage;
import com.app.library.utils.DialogUtil;
import com.app.library.utils.GlideUtils;
import com.app.library.utils.TimeUtil;
import com.app.library.utils.ToastUtil;
import com.app.library.utils.UserManager;
import com.app.library.widgts.CustomDialog;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class GoodsDetailActivity extends BaseActivity {


   private TextView tvTitle;

    private ImageView imgvPic;

    private TextView tvName;

    private  TextView tvPrice;

    private TextView tv_bt;

    private TextView tv_zh;

    private TextView tvUser;
    private TextView tvTime;
    private  TextView tvDescribe;
    private GoodsBean bean;
    private  Bundle bundle;
    private TextView tvRight;
    private TextView tvMobile;
    private  RecyclerView recy;
    private String dialog_title = "删除商品";
    private String dialog_content = "是否确定删除？";
    private CustomDialog customDialog;
    List<String> list_id = new ArrayList<>();
    TalkAdapter adapter;
    List<TalkBean> list = new ArrayList<>();
    List<TalkBean> list_all = new ArrayList<>();
    int status;
    int zhihuan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        recy= (RecyclerView) findViewById(R.id.recy);
        tvMobile= (TextView) findViewById(R.id.tv_mobile);
        tvRight= (TextView) findViewById(R.id.tv_add);
        tvDescribe= (TextView) findViewById(R.id.tv_describe);
        tvTime= (TextView) findViewById(R.id.tv_time);
        imgvPic= (ImageView) findViewById(R.id.imgv_pic);
        tvName= (TextView) findViewById(R.id.tv_goods_name);
        tvPrice= (TextView) findViewById(R.id.tv_price);
        tv_bt= (TextView) findViewById(R.id.tv_bt);
        tv_zh= (TextView) findViewById(R.id.tv_zh);
        tvUser= (TextView) findViewById(R.id.tv_user);
      ImageView  imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        TextView  tvmessage= (TextView) findViewById(R.id.tv_message);
        imgvreturn.setOnClickListener(this::onViewClicked);
        tv_bt.setOnClickListener(this::onViewClicked);
        tv_zh.setOnClickListener(this::onViewClicked);
        tvRight.setOnClickListener(this::onViewClicked);
        tvmessage.setOnClickListener(this::onViewClicked);

        tvTitle.setText("商品详情");
        initData();
        initTalk();
    }

    private void initTalk() {
        list_all = DataSupport.findAll(TalkBean.class);
        if (list_all.size() > 0) {
            for (int i = 0; i < list_all.size(); i++) {
                if (list_all.get(i).getQues_id().equals(bean.goods_id)) {
                    list.add(list_all.get(i));
                }
            }
        }

        Collections.reverse(list); // 倒序排列

        adapter = new TalkAdapter(R.layout.item_list_talk, list);
        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(adapter);

    }

    private void initData() {

        List<ShopCarBean> list = DataSupport.findAll(ShopCarBean.class);
        list_id.clear();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getCreate_user_id().equals(UserManager.getUserId(this))) {
                list_id.add(list.get(i).getGoods_id());
            }
        }

        bundle = getIntent().getExtras();
        bean = (GoodsBean) bundle.getSerializable("bean");
        setView();
    }

    private void setView() {
        if (bean.getCreate_user_id().equals(UserManager.getUserId(this))) {
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText("编辑");
        } else {
            tvRight.setVisibility(View.GONE);
        }
        GlideUtils.getInstance().loadImage(this, bean.getGood_pic_path(), imgvPic);
        tvName.setText("商品：" + bean.getGoods_name());
        tvPrice.setText("价格：¥" + bean.getGoods_price());
        tvUser.setText("发布者：" + bean.getCreate_user_name());
        tvMobile.setText("联系方式：" + bean.mobile);
        tvTime.setText("发布时间：" + bean.getCreate_time());
        tvDescribe.setText("卖家描述：" + bean.getRemark());

        status = bean.status;
        zhihuan = bean.zhihuan;


        if (bean.getCreate_user_id().equals(UserManager.getUserId(this))) {
            if (status == 1) {
                tv_bt.setText("点击发货");
            } else if (status == 2) {
                tv_bt.setText("已发货");
            } else if (status == 3) {
                tv_bt.setText("已完成");
            } else if (status == 0) {
                tv_bt.setText("下架商品");
            }

            tv_zh.setVisibility(View.GONE);
        } else {

            tv_zh.setVisibility(View.VISIBLE);
            //购买
            if (status == 0) {
                if (list_id.contains(bean.getGoods_id())) {
                    tv_bt.setText("已加入");
                } else {
                    tv_bt.setText("加入购物车");
                }
            } else {
                tv_bt.setText("已售出");
            }


            //1 置换中 2 拒绝  3 通过
            if (zhihuan == 0 || zhihuan == 2) {
                tv_zh.setText("点击置换");
            } else {
                if (zhihuan == 1) {
                    tv_zh.setText("置换中");
                } else if (zhihuan == 3) {
                    tv_zh.setText("已置换");
                }
            }
        }
    }


    private void add() {

        if (status == 0) {
            if (list_id.contains(bean.getGoods_id())) {
                ToastUtil.showToast(this, "购物车已有该商品");
                tv_bt.setText("已加入");
                return;
            }
            ShopCarBean shopCarBean = new ShopCarBean();
            shopCarBean.setS_id(System.currentTimeMillis() + "");
            shopCarBean.setGoods_id(bean.getGoods_id());
            shopCarBean.setGoods_name(bean.getGoods_name());
            shopCarBean.setGood_pic_path(bean.getGood_pic_path());
            shopCarBean.setGoods_price(bean.getGoods_price());
            shopCarBean.setCreate_user_id(UserManager.getUserId(this));
            shopCarBean.setCreate_time(TimeUtil.getTodayData("yyyy-MM-dd HH:mm:ss"));
            shopCarBean.save();
            list_id.add(bean.goods_id);
            tv_bt.setText("已加入");
            ToastUtil.showToast(this, "已加入购物车");
            EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
        } else {
            ToastUtil.showToast(this, "商品已售出");
        }
    }


    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
            case R.id.tv_add:

                if (!UserManager.isLogin(this)) {
                    ToastUtil.showToast(this, "请登陆");
                    return;
                }
                if (bean.status == 0 && (bean.zhihuan == 0 || bean.zhihuan == 2)) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bean", bean);
                    bundle.putInt("from", 1);
                    showActivity(this, AddGoodsActivity.class, bundle);
                } else {
                    ToastUtil.showToast(this, "买家已下单，无法修改");
                }
                break;
            case R.id.tv_bt:
                if (!UserManager.isLogin(this)) {
                    ToastUtil.showToast(this, "请登陆");
                    return;
                }

                if (UserManager.getUserId(this).equals(bean.getCreate_user_id())) {
                    if (status == 0) { //普通
                        del();
                    } else if (status == 1) { //点击发货
                        ContentValues values = new ContentValues();
                        values.put("status", 2);
                        DataSupport.updateAll(GoodsBean.class, values, "goods_id=?", bean.goods_id);
                        ToastUtil.showToast(this, "已发货");
                        tv_bt.setText("已完成");
                        EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));

                    } else {//已发货
                        ToastUtil.showToast(this, "已发货，无法操作");

                    }
                } else {
                    if (zhihuan == 0 || zhihuan == 2) {
                        add();
                    } else if (zhihuan == 1) {
                        ToastUtil.showToast(this, "商品置换中，无法购买");
                    } else if (zhihuan == 3) {
                        ToastUtil.showToast(this, "商品已置换，无法购买");
                    }
                }

                break;

            case R.id.tv_zh:
                if (status == 0) {
                    if (zhihuan == 0 || zhihuan == 2) {
                        //可以置换
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("bean", bean);
                        showActivity(this, ZHEditActivity.class, bundle);

                    } else {
                        ToastUtil.showToast(this, "商品目前处于置换中");
                    }
                } else {
                    ToastUtil.showToast(this, "商品售出，无法置换");

                }


                break;

            case R.id.tv_message:
                if (!UserManager.isLogin(this)) {
                    ToastUtil.showToast(this, "请登陆");
                    return;
                }
                showActivity(this, MessageActivity.class, bundle);
                break;
        }
    }

    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            if (msg.getmObject() != null) {
                bean = (GoodsBean) msg.getmObject();
                setView();
            }
        }
    }

    private void del() {

        customDialog = DialogUtil.showDialog(this, customDialog, 2, dialog_title, dialog_content, "取消", "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

                DataSupport.deleteAll(GoodsBean.class, "goods_id=?", bean.getGoods_id());
                EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
                ToastUtil.showToast(GoodsDetailActivity.this, "删除成功");
                finish();
            }
        });

        if (customDialog != null && !customDialog.isShowing()) {
            customDialog.show();
        }


        customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                customDialog = null;
            }
        });

    }
}
